﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Yule8xCommon.Applib
{
    public class StringUtil
    {
        public static int ToInteger(string value, int defv = 0)
        {
            int vout;
            return int.TryParse(value, out vout) ? vout : defv;
        }
        public static decimal ToDecimal(string value, decimal defv = 0)
        {
            decimal vout;
            return decimal.TryParse(value, out vout) ? vout : defv;
        }
        public static double ToDecimal(string value, double defv = 0)
        {
            double vout;
            return double.TryParse(value, out vout) ? vout : defv;
        }

        public static string Substring(object value, int length, string defv)
        {
            if (DBNull.Value == value || null == value)
            {
                return defv;
            }
            string str = value.ToString();
            if (str.Length < 1)
            {
                return defv;
            }
            return str.Length < length ? str : str.Substring(0, length);
        }
    }
}